import tools.instantiation;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@WebServlet("/back_login")
public class login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request,response);

    }
    public void wrong1(){//对话框提示信息
        String msg="用户名不能为空！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong2(){
        String msg="用户密码不能为空，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong3(){
        String msg="该用户尚未注册，登录失败！";
        int type= JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong4(){
        String msg="用户密码不正确，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String userName=new String(request.getParameter("text").getBytes("ISO-8859-1"),"UTF-8");
        String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        System.out.println(userName);
        if(userName.equals("")){
            wrong1();
            System.out.println("登陆失财");
            response.sendRedirect("http://localhost:8080/login.html");
        }else if(password.equals("")){
            wrong2();
            System.out.println("登陆失财");
            response.sendRedirect("http://localhost:8080/login.html");
        }else{
            try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("org.h2.Driver");
                /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
                String url="jdbc:h2:~/test1";
                System.out.println("到达");
                con= DriverManager.getConnection(url,"root","root");
                System.out.println("到达2");
                stmt=con.createStatement();
                String sql="select * from shop_user where username='"+userName+"' and role = '0'";
                rs=stmt.executeQuery(sql);
                int N=0;
                int P=0;
                while(rs.next()){
                    if(userName.equals(rs.getString("username"))){
                        N=111111;
                        if(password.equals(rs.getString("password"))){
                            P=111111;
                            //开始实例化usernamesession
                            instantiation.setSeesion(rs,1,request,response);
                            System.out.println("登陆成功");
                            response.sendRedirect("http://localhost:8080/index.jsp");
                        }else{

                        }
                    }else{
                        N++;
                    }
                }
                if(N<111111){
                    wrong3();
                    System.out.println("登陆失财");
                    response.sendRedirect("http://localhost:8080/login.html");
                }else if(P<111111){
                    wrong4();
                    System.out.println("登陆失财");
                    response.sendRedirect("http://localhost:8080/login.html");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
